<template>
  <el-row justify="end">
    <el-button @click="openForm">
      <Icon class="mr-5px" icon="ep:edit" />
      {{ environment.id ? '编辑环保信息' : '新增环保信息' }}
    </el-button>
  </el-row>
  
  <label class="font-bold">废水（漂洗水）种类</label>
  <EnvironmentWastewaterList
    :key="environmentWastewaterListRefKey"
    ref="environmentWastewaterList"
    :environment="environment"
  />
  <label class="font-bold">高浓废液种类</label>
  <EnvironmentHighWastewaterList
    :key="environmentHighWastewaterListRefKey"
    ref="environmentHighWastewaterListRef"
    :environment="environment"
  />
  <label class="font-bold">废气塔/废气种类</label>
  <EnvironmentWastegasList
    :key="environmentWastegasListRefKey"
    ref="environmentWastegasListRef"
    :environment="environment"
  />
  <label class="font-bold">危废种类</label>
  <EnvironmentWasteList
    :key="environmentWasteListRefKey"
    ref="environmentWasteListRef"
    :environment="environment"
  />

  <ContentWrap>
    <el-collapse v-model="activeNames" class="">
      <el-collapse-item name="systemInfo">
        <template #title>
          <span class="text-base font-bold">系统信息</span>
        </template>
        <el-descriptions :column="4">
          <el-descriptions-item label="创建人">{{ environment.creatorName }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">
            {{ formatDate(environment.createTime) }}
          </el-descriptions-item>
          <el-descriptions-item label="修改人">{{ environment.updaterName }}</el-descriptions-item>
          <el-descriptions-item label="修改时间">
            {{ formatDate(environment.updateTime) }}
          </el-descriptions-item>
        </el-descriptions>
      </el-collapse-item>
    </el-collapse>
  </ContentWrap>
    <!-- 表单弹窗：添加 -->
    <EnvironmentForm ref="formRef" @success="refresh" />
</template>
<script lang="ts" setup>
import { EnvironmentApi, EnvironmentVO } from '@/api/crm/environment'
import * as CustomerApi from '@/api/crm/customer'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import EnvironmentWastegasList from '@/views/crm/environment/components/EnvironmentWastegasList.vue'
import EnvironmentWasteList from '@/views/crm/environment/components/EnvironmentWasteList.vue'
import EnvironmentWastewaterList from '@/views/crm/environment/components/EnvironmentWastewaterList.vue'
import EnvironmentHighWastewaterList from '@/views/crm/environment/components/EnvironmentHighWastewaterList.vue'
import { formatDate } from '@/utils/formatTime'
import EnvironmentForm from '@/views/crm/environment/EnvironmentForm.vue'
import { BizTypeEnum } from '@/api/crm/permission'
import type { OperateLogVO } from '@/api/system/operatelog'
import { getOperateLogPage } from '@/api/crm/operateLog'

const formRef = ref()
const environmentWastegasListRefKey = ref(new Date().getTime())
const environmentWasteListRefKey = ref(new Date().getTime())
const environmentWastewaterListRefKey = ref(new Date().getTime())
const environmentHighWastewaterListRefKey = ref(new Date().getTime())
const environmentWasteWarehouseListRefKey = ref(new Date().getTime())
const openForm = () => {
  formRef.value.open(environment.value.id ? 'update' : 'create', environment.value.id, props.bizId)
}
defineOptions({ name: 'EnvironmentDetail' })
const props = defineProps<{ id?: number,bizId?: number }>()
const route = useRoute();

const formLoading = ref(false) // 表单的加载中
const environment = ref<EnvironmentVO>({} as EnvironmentVO) // 企业工商信息详情

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.bizId,
  async (val) => {
    if (!val) {
      return
    }
    try {
      formLoading.value = true
      const data = await EnvironmentApi.getEnvironmentByCustomerId(val)
      if (!data) {
        return
      }
      environment.value = data
      await getOperateLog()
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)
const activeNames = ref(['wasteGasInfo','strippingMethodInfo', 'systemInfo']) // 展示的折叠面板
const refresh = async () => {
environmentWastegasListRefKey.value = new Date().getTime()
environmentWasteListRefKey.value = new Date().getTime()
environmentWastewaterListRefKey.value = new Date().getTime()
environmentHighWastewaterListRefKey.value = new Date().getTime()
environmentWasteWarehouseListRefKey.value = new Date().getTime()
  try {
    formLoading.value = true
    const data = await EnvironmentApi.getEnvironmentByCustomerId(props.bizId)
    if (!data) {
      return
    }
    environment.value = data
    await getOperateLog()
  } finally {
    formLoading.value = false
  }
}

/** 获取操作日志 */
const logList = ref<OperateLogVO[]>([]) // 操作日志列表
const getOperateLog = async () => {
  if (!environment.value.id) {
    return
  }
  const data = await getOperateLogPage({
    bizType: BizTypeEnum.CRM_ENVIRONMENT,
    bizId: environment.value.id
  })
  logList.value = data.list
}

/** 初始化 */
onMounted(async () => {
  const id = props.id || route.params.id
  const customerId = props.bizId
  try {
    formLoading.value = true
    if(customerId){
      const data = await EnvironmentApi.getEnvironmentByCustomerId(customerId)
      if (!data) {
        return
      }
      environment.value = data
    } else {
      if(id){
        const data = await EnvironmentApi.getEnvironment(id)
        if (!data) {
          return
        }
        environment.value = data
      }
    }
    await getOperateLog()
  } finally {
    formLoading.value = false
  }
})
</script>
